package com.woniu.smart.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.PublicResourceDTO;
import com.woniu.smart.entity.Publicresource;
import com.woniu.smart.service.PublicresourceService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.PublicresourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公共资源的控制层
 *
 * @author 晨星
 * @date 2022/10/20/14:32
 */
@RestController
@RequestMapping("publicresource")
public class PublicresourceController {

    @Autowired
    private PublicresourceService publicresourceService;

    /**
     * 查询全部公共资源
     *
     * @author 晨星
     * @date 2022/10/20 14:38
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("queryAll")
    public Result queryAllPublicresource(@RequestBody PublicResourceDTO publicResourceDTO){
        List<PublicresourceVo> list = publicresourceService.selectAll(publicResourceDTO);
        PageInfo<PublicresourceVo> pageInfo = new PageInfo<>(list);
        return Result.ok().data("list",pageInfo);
    }

    /**
     * 根据id查询公共资源
     *
     * @author 晨星
     * @date 2022/10/20 21:40
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("{id}")
    public Result queryByPublicresourceId(@PathVariable Long id){
        Publicresource publicresource = publicresourceService.selectByPrimaryKey(id);
        return Result.ok().data("publicresource",publicresource);
    }

    /**
     * 新增公共资源信息
     *
     * @author 晨星
     * @date 2022/10/21 14:31
     * @param publicresource 公共资源的实体类
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("")
    public Result insertPublicresource(@RequestBody Publicresource publicresource){
        publicresourceService.insertSelective(publicresource);
        return Result.ok();
    }

    /**
     * 修改公共资源信息
     *
     * @author 晨星
     * @date 2022/10/20 21:44
     * @param publicresource 公共资源的实体类
     * @return com.woniu.smart.util.Result
     **/
    @PutMapping("")
    public Result updatePublicresource(@RequestBody Publicresource publicresource){
        publicresourceService.updateByPrimaryKeySelective(publicresource);
        return Result.ok();
    }

    /**
     * 根据id删除公共资源
     *
     * @author 晨星
     * @date 2022/10/21 14:31
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @DeleteMapping("{id}")
    public Result deletePublicresourceById(@PathVariable Long id){
        publicresourceService.deleteByPrimaryKey(id);
        return Result.ok();
    }

    /**
     * 根据id批量删除公共资源
     *
     * @author 晨星
     * @date 2022/10/24 17:31
     * @param publicresourceIds 批量的公共资源主键id
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping ("deleteBatch")
    public Result deleteBatch(@RequestParam("publicresourceIds") List<Integer> publicresourceIds){
        publicresourceService.deleteBatch(publicresourceIds);
        return Result.ok();
    }
}
